/*
 * @Author: intellectual-seeker 3336339701@qq.com
 * @Date: 2025-03-24 14:52:20
 * @LastEditors: intellectual-seeker 3336339701@qq.com
 * @LastEditTime: 2025-03-24 16:49:06
 * @FilePath: /code/lesson25/named_pipe/comm.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#ifndef COMM_HPP
#define COMM_HPP

#define Path "./fifo"
#define Mode 0666

#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <cerrno>
#include <fcntl.h>

using namespace std;

class Fifo
{
public:
    Fifo(const string& path)
        :_path(path)
    {
        umask(0);
        int n = mkfifo(_path.c_str(), Mode);

        if(n == 0)
        {
            cout << "mkfifo success" << endl;
        }
        else
        {
            cerr << "mkfifo failed " << errno << " errstring: " << strerror(errno) << endl;
        }
    }

    ~Fifo()
    {
        int n = unlink(_path.c_str());

        if(n == 0)
        {
            cout << "remove fifo file " << _path << " success" << endl;
        }
        else
        {
            cerr << "remove failed " << errno << " errstring: " << strerror(errno) << endl;
        }
    }

private:
    string _path;
};

#endif
